package com.yellow.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class AliyunSMSUtis {
    public static final String VALIDATE_CODE = "SMS_176942109";//发送短信验证码
    public static final String ORDER_NOTICE = "SMS_176937736";//体检预约成功通知

    private static String accessKeyId;
    private static String accessKeySecret;

    private AliyunSMSUtis() {
    }

    static {
        //加载prop
        InputStream is = AliyunOSSUtils.class.getClassLoader().getResourceAsStream("aliyun.properties");
        Properties prop = new Properties();
        try {
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        accessKeyId = prop.getProperty("accessKeyId");
        accessKeySecret = prop.getProperty("accessKeySecret");
    }

    public static String SendSMS(String template, String number, String code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-chengdu", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", number);
        request.putQueryParameter("SignName", "健康生活");
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("TemplateParam", "{code:" + code + "}");
        CommonResponse response = client.getCommonResponse(request);
        return response.getData();
    }

}
